Color reduction is the process of reducing the number of colors present in an image to improve display or file handling. Generally the starting image is a 24-bit RGB image that can have as many as 16 million colors. The resulting image is an 8-bit palette color image with 256 or fewer colors.
The main reason to use color reduction is to cut down the image storage requirements. Converting an image from 24-bit to 8-bit reduces the space required to store the image to one-third of the original.
In a Windows application the use of color reduction for image display avoids the painfully slow and poorly rendered display of 24-bit RGB images on display adapters limited to 256 (or fewer) colors.
When reducing the number of colors in the image you have control over the type of palette used for the resulting image . . .
You also have control over the pixel pattern used to disperse the color . . .
You can apply color reduction to 24-bit RGB images or to 8-bit palette color images.
Table 1. Victor Color Reduction Functions Color Reduction -- Starting -- -------------- Resulting Image ---------------- Function bits per pixel Palette Number of Colors Pixel Pattern --------------- -------------- --------- ---------------- ----------------- colordither 8- or 24-bit standard 16 dither colordither 8- or 24-bit rainbow 256 dither colorscatter 8- or 24-bit standard 16 diffusion scatter colorscatter 8- or 24-bit rainbow 256 diffusion scatter colortogray 8- or 24-bit grayscale 256 none convertrgbtopal 24-bit optimum 2 to 256 none convertrgbtopalex 24-bit optimum 2 to 256 none or diffusion scatter matchcolorimage 8- or 24-bit specific 2 to 256 diffusion scatter matchcolorimageex 8- or 24-bit specific 2 to 256 none or diffusion scatter reduceimagecolors 8-bit optimum 2 to 255 diffusion scatter ----------------------------------------------------------------------------------Victor also has automatic color reduction built into the viewimageex function (not available in Dos version). So you can display a 24-bit RGB image without going to the trouble of making an 8-bit version of it.
// Allocate an 8-bit image allocimage(&image8, (int)rgbimage.bmh->biWidth, (int)rgbimage.bmh->biHeight, 8); // Convert RGB to 8-bit image with 256 colors convertrgbtopal(256, &rgbimage, &image8); savepcx("ANY.PCX", &image8); // Save the image as a PCX file freeimage(&image8);
Where rgbimage is a 24-bit RGB color image and image8 is an 8-bit palette color image.
// Allocate an 8-bit image allocimage(&image8, (int)rgbimage.bmh->biWidth, (int)rgbimage.bmh->biHeight, 8); // Copy an existing palette to the new image copyimagepalette(&anotherimage, &image8); // Convert RGB to 8-bit image matchcolorimage(&rgbimage, &image8); savepcx("NEW.PCX", &image8); // Save the image as PCX file freeimage(&image8);
Where rgbimage is a 24-bit RGB color image, anotherimage is an existing image with the palette of interest, and image8 is an 8-bit palette color image.
A Victor image can be displayed easily with the viewimageex function which automatically uses color reduction to display a 24-bit image in a display mode limited to 256 or fewer colors. The rainbow palette and scatter pattern are selected by the defined constant VIEWSCATTER.
HDC hDC; PAINTSTRUCT ps; HPALETTE hpal; hDC = BeginPaint(hWnd, &ps); // Display the image beginning at (0,0) in the image // and place it at (0,0) in the window viewimageex(hWnd, hDC, &hpal, 0, 0, &rgbimage, 0, 0, VIEWSCATTER); EndPaint(hWnd, &ps);
The viewimageex function also lets you specify the starting position for the image and the scroll positions.
Victor Image Processing Library homepage | Victor Application Notes